var _global = require('global/global.js');
var TWEEN = require('third_party/tweenjs/tween.js');
var ChangingScene = require('scenes/module/switchscene.js');
var _Event = require('component/event.js');
//var GC = require('nativeclasses').gcobserver;

var MODE_IN = 0;
var MODE_OUT = 1;

var _BackListener = require('global/backlistener.js');

function AnimaConfig(current, next) {
    this.current = current;
    this.next = next;
    this.mAll = new ChangingScene();

    this.mDirection = 'up';// new scene is in down direction
    this.mMode = MODE_IN;
    this.mOver = false;

    this.mStartGc = false;
    this.mEndGc = true;
    this.mEasing = TWEEN.Easing.Cubic.InOut;
}
/**
 * @param mode
 * @param over if true, underlying layer will not move
 */
AnimaConfig.prototype.setMode = function (isin, over) {
    this.mMode = isin ? MODE_IN : MODE_OUT;
    this.mOver = over;
    return this;
};
AnimaConfig.prototype.direction = function (d) {
    this.mDirection = d;
    return this;
};
AnimaConfig.prototype.gcOnStart = function (gc) {
    this.mStartGc = gc;
    return this;
};
AnimaConfig.prototype.gcOnEnd = function (gc) {
    this.mEndGc = gc;
    return this;
};
AnimaConfig.prototype.easing = function (e) {
    this.mEasing = e;
    return this;
};
/**
 * @param scene switch scene
 */
AnimaConfig.prototype.__onShow = function () {
    _global.unregisterScene(this.mAll, this.mEndGc);

    var target = this.next;
    if (this.mOver && this.mMode == MODE_OUT) {
        target = this.current;
    }

    target.setPosition(0, 0);
    _global.registerScene(target);
    target.setTouchEnable(true);
    target.onEnter();

    _global.scheduleTask(function () {
        _global.reclock();
    });

    this.next = null;
    this.current = null;
    this.mAll = null;
};
AnimaConfig.prototype.start = function () {
    var current = this.current;
    var next = this.next;

    var screen = _global.screen;
    next.onSizeChange(screen.width(), screen.height());

    // ------------ show menu
    current.setTouchEnable(false);
    next.setTouchEnable(false);

    var fromx = 0;
    var fromy = 0;
    // init as if over is false
    switch (this.mDirection) {
        case 'down':
            fromx = 0;
            fromy = 1;
            break;
        case 'up':
            fromx = 0;
            fromy = -1;
            break;
        case 'left':
            fromx = 1;
            fromy = 0;
            break;
        case 'right':
            fromx = -1;
            fromy = 0;
            break;
    }

    var listener = _Event.createListener(this.__onShow, this);
    var anima;

    // setup scene position
    this.mAll.addScene(current, 0, 0);
    if (this.mOver) {
        if (this.mMode == MODE_IN) {
            this.mAll.addScene(next, fromx, fromy);
            anima = this.mAll.createAnima(next, listener)
                .setStart(fromx, fromy)
                .setDest(0, 0);
        } else {// slide off
            this.mAll.addScene(next, 0, 0);
            anima = this.mAll.createAnima(next, listener)
                .setStart(0, 0)
                .setDest(-fromx, -fromy);
        }
    } else {
        this.mAll.addScene(next, fromx, fromy);
        anima = this.mAll.createAnima(this.mAll, listener)
            .setStart(0, 0)
            .setDest(-fromx, -fromy);
    }
    anima.setTime(0.5).setEasing(this.mEasing).start();

    // unregister current
    _BackListener.setBlocking(true);
    if (this.mOver && this.mMode == MODE_OUT) {
        next.onExit();
        _global.unregisterScene(next, this.mStartGc);
    } else {
        current.onExit();
        _global.unregisterScene(current, this.mStartGc);
    }

    _global.registerScene(this.mAll);
    _global.scheduleTask(function () {
        _global.reclock();
    })
};

exports.createAnima = function (current, next) {
    return new AnimaConfig(current, next);
};